package com.itheima.reggie.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.config.common.R;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.dto.PageQueryDto;
import com.itheima.reggie.entity.dto.SetmealDto;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;

/**
 * 套餐管理
 *
 * @author BaiXin
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    /**
     * 套餐添加
     *
     * @param setmealDto 套餐数据
     * @return
     */
    @CacheEvict(cacheNames = "setmeal", allEntries = true)
    @PostMapping
    public R save(@RequestBody SetmealDto setmealDto) {
        //日志
        log.debug("保存套餐数据：[{}]", setmealDto);

        //校验：无

        //调用service保存
        boolean result = setmealService.saveWithDish(setmealDto);
        //返回数据
        if (result) {
            return R.success("套餐添加成功");
        }
        return R.error("套餐添加失败");
    }

    /**
     * 分页查询 包含套餐分类名称
     *
     * @param pageQueryDto
     * @return
     */
    @GetMapping("/page")
    public R<Page<SetmealDto>> page(PageQueryDto pageQueryDto) {
        //日志
        log.debug("分页查询数据：[{}]", pageQueryDto);

        //参数校验：no

        //调用service方法查询
        Page<SetmealDto> page = setmealService.page(pageQueryDto);
        //返回数据
        return R.success(page);
    }


    /**
     * 根据ids批量删除
     *
     * @param ids
     * @return
     */
    @CacheEvict(cacheNames = "setmeal", allEntries = true)
    @DeleteMapping
    public R deleteByIds(Long[] ids) {
        //日志
        log.debug("根据ids删除，删除的ids为：[{}]", Arrays.toString(ids));
        //校验：no

        //调用service删除
        boolean result = setmealService.deleteByIdsWithSetmealDish(Arrays.asList(ids));

        //返回数据
        if (result) {
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }


    /**
     * 批量修改状态
     *
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R updateStatusByIds(@PathVariable Integer status, Long[] ids) {
        log.debug("批量修改状态，状态[{}],菜品id[{}]", status, ids);

        //校验，status只能是0或者1
        if (status != 0 && status != 1) {
            return R.error("目标状态错误");
        }

        //调用service
        boolean result = setmealService.switchStatus(status, ids);
        //组织数据返回
        if (result) {
            return R.success("修改状态成功");
        }
        return R.error("修改失败");
    }

    /**
     * 通用条件查询
     *
     * @param setmeal
     * @return
     */
    @Cacheable(value = "setmeal",key = "#setmeal.categoryId + '_' + #setmeal.status")
    @GetMapping("/list")
    public R<List<Setmeal>> findByCondition(Setmeal setmeal) {
        //日志
        log.debug("查询套餐，条件：[{}]", setmeal);

        //参数校验

        //调用service
        List<Setmeal> setmeals = setmealService.findByCondition(setmeal);

        //返回数据
        return R.success(setmeals);
    }
}    